package com.meeting.master.service.base;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;

import com.meeting.master.manager.bo.*;
import com.meeting.master.manager.bo.UserInfoBO;
import com.meeting.master.persist.dos.UserInfo;
import com.meeting.master.service.base.BaseUserInfoBOService.CreateUserInfoBoResult;
import com.meeting.master.service.bto.CreateUserInfoBto;
import com.vs.bo.AddedBto;
import com.vs.bo.BaseBoResult;
import com.vs.bo.UnmodifiedBto;
import com.vs.bo.UpdatedBto;
import com.vs.code.AutoGenerated;
import com.vs.code.DoNotModify;
import com.vs.ox.common.exception.IgnoredException;
import com.vs.sqlmapper.core.PgIdGenerator;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.util.Set;

import javax.annotation.Resource;

@DoNotModify
@Slf4j
@Component
@AutoGenerated(locked = true, uuid = "a0b6a03c-f848-38ea-88ed-80195c0a9217")
public class BaseUserInfoBOService {
    @AutoGenerated(locked = true)
    @Resource(name = "pgIdGenerator")
    private PgIdGenerator idGenerator;

    /** 数据库创建一行, 数据库创建一行 */
    @AutoGenerated(locked = true)
    private UserInfoBO createCreateUserInfo(
            BaseUserInfoBOService.CreateUserInfoBoResult boResult,
            CreateUserInfoBto createUserInfoBto) {
        UserInfoBO userInfoBO = null;
        boolean found = false;
        boolean allNull = false;
        boolean pkMatched = false;
        boolean pkExist = false;
        String matchedUkName = "";
        if (userInfoBO != null) {
            if (pkMatched) {
                log.error("主键冲突, id:{}的记录在数据库表:{}中已经存在!", userInfoBO.getId(), "master_user_info");
                throw new IgnoredException(400, "ユーザ已存在");
            } else {
                log.error(
                        "唯一键UK{}和数据库表:'{}'中id为:{}的记录冲突",
                        matchedUkName,
                        "master_user_info",
                        userInfoBO.getId());
                throw new IgnoredException(400, "ユーザ已存在");
            }
        } else {
            userInfoBO = new UserInfoBO();
            if (pkExist) {
                userInfoBO.setId(this.idGenerator.allocateId("master_user_info"));
            } else {
                userInfoBO.setId(this.idGenerator.allocateId("master_user_info"));
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createUserInfoBto, "__$validPropertySet"),
                    "name")) {
                userInfoBO.setName(createUserInfoBto.getName());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createUserInfoBto, "__$validPropertySet"),
                    "gender")) {
                userInfoBO.setGender(createUserInfoBto.getGender());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createUserInfoBto, "__$validPropertySet"),
                    "telNum")) {
                userInfoBO.setTelNum(createUserInfoBto.getTelNum());
            }
            AddedBto addedBto = new AddedBto();
            addedBto.setBto(createUserInfoBto);
            addedBto.setBo(userInfoBO);
            boResult.getAddBtoList().add(addedBto);
        }
        return userInfoBO;
    }

    /** 创建用户信息 */
    @AutoGenerated(locked = true)
    protected CreateUserInfoBoResult createUserInfoBase(CreateUserInfoBto createUserInfoBto) {
        if (createUserInfoBto == null) {
            throw new IgnoredException(400, "bto 不能为null!");
        }
        CreateUserInfoBoResult boResult = new CreateUserInfoBoResult();
        UserInfoBO userInfoBO = createCreateUserInfo(boResult, createUserInfoBto);
        boResult.setRootBo(userInfoBO);
        return boResult;
    }

    public static class CreateUserInfoBoResult extends BaseBoResult {

        @AutoGenerated(locked = true)
        public UserInfoBO getRootBo() {
            return (UserInfoBO) this.rootBo;
        }

        @AutoGenerated(locked = true)
        public AddedBto<CreateUserInfoBto, UserInfoBO> getCreatedBto(
                CreateUserInfoBto createUserInfoBto) {
            return this.getAddedResult(createUserInfoBto);
        }

        @AutoGenerated(locked = true)
        public UserInfo getDeleted_UserInfo() {
            return (UserInfo) CollectionUtil.getFirst(this.getDeletedEntityList(UserInfo.class));
        }

        @AutoGenerated(locked = true)
        public UpdatedBto<CreateUserInfoBto, UserInfo, UserInfoBO> getUpdatedBto(
                CreateUserInfoBto createUserInfoBto) {
            return super.getUpdatedResult(createUserInfoBto);
        }

        @AutoGenerated(locked = true)
        public UnmodifiedBto<CreateUserInfoBto, UserInfoBO> getUnmodifiedBto(
                CreateUserInfoBto createUserInfoBto) {
            return super.getUnmodifiedResult(createUserInfoBto);
        }
    }
}
